package ui.activity.company;

import android.app.Activity;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;

import com.trek2000.android.enterprise.Enterprise;
import com.trek2000.android.enterprise.R;

import controller.company.AllCompanyFeedAsync;
import controller.company.CompanyFeedAsync;
import controller.company.DownloadFileAsync;
import controller.company.DownloadImageInPreviewCompanyFeedAsync;
import define.API;
import define.Key;

/**
 * Created by Dong Le on 5/27/2015.
 */
public class PreviewCompanyFeed extends Activity implements View.OnClickListener, View.OnTouchListener{

    //string session
    private FrameLayout mFlInPreviewCompanyFeed;
    private ImageButton mIbtnPlayVideoInPreviewCompanyFeed;
    private ImageButton mIbtnBackInPreviewCompanyFeed;
    private ImageView mIvPhotoInPreviewCompanyFeed;
    private ImageView mIvPreviewVideoInPreviewCompanyFeed;
    private LinearLayout mLlTopInPreviewCompanyFeed;
    private LinearLayout mLlBottomInPreviewCompanyFeed;
    private TextView mTvDownloadInPreviewCompanyFeed;
    private TextView mTvPositionOfFileInPreviewCompanyFeed;
    private TextView mTvUserNameInPreviewCompanyFeed;
    private VideoView mVvVideoInPreviewCompanyFeed;
    //the others session
    Handler mHandler = new Handler();

    Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            hideTopAndBottomLlInPreviewCompanyFeed();
            mHandler.removeCallbacks(this);
        }
    };
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ibtn_back_in_preview_company_feed_activity:
                finish();
                break;
            case R.id.tv_download_in_preview_company_feed_activity:
                // TODO download feature on here
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
                    new DownloadFileAsync(this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
                            CompanyFeedAsync.mAlCompanyFeed.get(Enterprise.company.getPositionOfCompanyFeedItem()).
                                    getmAlShotOfCompanyFeedItem().get(Enterprise.company.getPositionOfGridViewOfCompanyFeedItem()).
                                    getPreviewMedium());
                else
                    new DownloadFileAsync(this).execute(
                            CompanyFeedAsync.mAlCompanyFeed.get(Enterprise.company.getPositionOfCompanyFeedItem()).
                                    getmAlShotOfCompanyFeedItem().get(Enterprise.company.getPositionOfGridViewOfCompanyFeedItem()).
                                    getPreviewMedium());
                break;
            case R.id.ibtn_play_video_in_preview_company_feed_activity:
                mIvPreviewVideoInPreviewCompanyFeed.setVisibility(View.GONE);
                mIbtnPlayVideoInPreviewCompanyFeed.setVisibility(View.GONE);
                mVvVideoInPreviewCompanyFeed.setVisibility(View.VISIBLE);
                String URL = "";
                if (API.IS_DEV_SITE) {
                    URL =  CompanyFeedAsync.mAlCompanyFeed.get(Enterprise.company.getPositionOfCompanyFeedItem()).
                            getmAlShotOfCompanyFeedItem().get(Enterprise.company.getPositionOfGridViewOfCompanyFeedItem()).
                            getPreviewMedium().replace("https","http");
                } else {
                    URL =  CompanyFeedAsync.mAlCompanyFeed.get(Enterprise.company.getPositionOfCompanyFeedItem()).
                            getmAlShotOfCompanyFeedItem().get(Enterprise.company.getPositionOfGridViewOfCompanyFeedItem()).
                            getPreviewMedium();
                }
                playVideoInPreviewCompanyFeed(URL);
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_preview_company_feed);

        initialView();;
        initialData();
        setDataForPreviewCompanyFeed();
        hideTopAndBottomLlInPreviewCompanyFeed();
    }

    @Override
    public boolean onTouch(View v, MotionEvent motionEvent) {
        switch (v.getId()) {
            case R.id.fl_in_preview_company_feed_activity:
                showTopAndBottomLlInPreviewCompanyFeed();
                mHandler.postDelayed(mRunnable, 3000);
                break;
        }
        return false;
    }

    private void initialView () {
        mFlInPreviewCompanyFeed = (FrameLayout) findViewById(R.id.fl_in_preview_company_feed_activity);
        mIbtnPlayVideoInPreviewCompanyFeed = (ImageButton) findViewById(R.id.ibtn_play_video_in_preview_company_feed_activity);
        mIbtnBackInPreviewCompanyFeed = (ImageButton) findViewById(R.id.ibtn_back_in_preview_company_feed_activity);
        mIvPhotoInPreviewCompanyFeed = (ImageView) findViewById(R.id.iv_photo_in_preview_company_feed_activity);
        mIvPreviewVideoInPreviewCompanyFeed = (ImageView) findViewById(R.id.iv_preview_video_in_preview_company_feed_activity);
        mLlTopInPreviewCompanyFeed = (LinearLayout) findViewById(R.id.ll_top_in_preview_company_feed_activity);
        mLlBottomInPreviewCompanyFeed = (LinearLayout) findViewById(R.id.ll_bottom_in_preview_company_feed_activity);
        mTvDownloadInPreviewCompanyFeed = (TextView) findViewById(R.id.tv_download_in_preview_company_feed_activity);
        mTvPositionOfFileInPreviewCompanyFeed = (TextView) findViewById(R.id.tv_position_of_file_in_preview_company_feed_activity);
        mTvUserNameInPreviewCompanyFeed = (TextView) findViewById(R.id.tv_user_name_in_preview_company_feed_activity);
        mVvVideoInPreviewCompanyFeed = (VideoView) findViewById(R.id.vv_video_in_preview_company_feed_activity);
    }

    private void initialData () {
        mIbtnBackInPreviewCompanyFeed.setOnClickListener(this);
        mTvDownloadInPreviewCompanyFeed.setOnClickListener(this);
        mFlInPreviewCompanyFeed.setOnTouchListener(this);
        mIbtnPlayVideoInPreviewCompanyFeed.setOnClickListener(this);
        // set initial for video view
        MediaController mMc = new MediaController(this);
        mMc.setAnchorView(mVvVideoInPreviewCompanyFeed);
        mMc.setMediaPlayer(mVvVideoInPreviewCompanyFeed);
        mVvVideoInPreviewCompanyFeed.requestFocus();
        mVvVideoInPreviewCompanyFeed.setBackgroundColor(Color.BLACK);
        mVvVideoInPreviewCompanyFeed.setMediaController(mMc);
        mVvVideoInPreviewCompanyFeed.setZOrderOnTop(true);
        mVvVideoInPreviewCompanyFeed.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (mVvVideoInPreviewCompanyFeed.isPlaying()) {
                    mVvVideoInPreviewCompanyFeed.pause();
                } else {
                    mVvVideoInPreviewCompanyFeed.start();
                }

                return false;
            }
        });
    }

    private void setDataForPreviewCompanyFeed () {
        if (CompanyFeedAsync.mAlCompanyFeed.get(Enterprise.company.getPositionOfCompanyFeedItem()).
                getmAlShotOfCompanyFeedItem().get(Enterprise.company.getPositionOfGridViewOfCompanyFeedItem()).
                getContentType() == Key.photo) {
            mIvPhotoInPreviewCompanyFeed.setVisibility(View.VISIBLE);
            mIvPreviewVideoInPreviewCompanyFeed.setVisibility(View.GONE);
            mIbtnPlayVideoInPreviewCompanyFeed.setVisibility(View.GONE);
            mVvVideoInPreviewCompanyFeed.setVisibility(View.GONE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
                new DownloadImageInPreviewCompanyFeedAsync(this, mIvPhotoInPreviewCompanyFeed).
                        executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
                                CompanyFeedAsync.mAlCompanyFeed.get(Enterprise.company.getPositionOfCompanyFeedItem()).
                                        getmAlShotOfCompanyFeedItem().get(Enterprise.company.getPositionOfGridViewOfCompanyFeedItem()).
                                        getPreviewMedium());
            else
                new DownloadImageInPreviewCompanyFeedAsync(this, mIvPhotoInPreviewCompanyFeed).
                        execute(
                                CompanyFeedAsync.mAlCompanyFeed.get(Enterprise.company.getPositionOfCompanyFeedItem()).
                                        getmAlShotOfCompanyFeedItem().get(Enterprise.company.getPositionOfGridViewOfCompanyFeedItem()).
                                        getPreviewMedium());
        } else if (CompanyFeedAsync.mAlCompanyFeed.get(Enterprise.company.getPositionOfCompanyFeedItem()).
                getmAlShotOfCompanyFeedItem().get(Enterprise.company.getPositionOfGridViewOfCompanyFeedItem()).
                getContentType() == Key.video) {
            mIvPhotoInPreviewCompanyFeed.setVisibility(View.GONE);
            mIvPreviewVideoInPreviewCompanyFeed.setVisibility(View.VISIBLE);
            mIbtnPlayVideoInPreviewCompanyFeed.setVisibility(View.VISIBLE);
            mVvVideoInPreviewCompanyFeed.setVisibility(View.GONE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
                new DownloadImageInPreviewCompanyFeedAsync(this, mIvPreviewVideoInPreviewCompanyFeed).
                        executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
                                CompanyFeedAsync.mAlCompanyFeed.get(Enterprise.company.getPositionOfCompanyFeedItem()).
                                        getmAlShotOfCompanyFeedItem().get(Enterprise.company.getPositionOfGridViewOfCompanyFeedItem()).
                                        getSplash());
            else
                new DownloadImageInPreviewCompanyFeedAsync(this, mIvPreviewVideoInPreviewCompanyFeed).
                        execute(
                                CompanyFeedAsync.mAlCompanyFeed.get(Enterprise.company.getPositionOfCompanyFeedItem()).
                                        getmAlShotOfCompanyFeedItem().get(Enterprise.company.getPositionOfGridViewOfCompanyFeedItem()).
                                        getSplash());
        }
        mTvPositionOfFileInPreviewCompanyFeed.setText(String.valueOf(Enterprise.company.getPositionOfGridViewOfCompanyFeedItem() + 1) + " of " +
                String.valueOf(CompanyFeedAsync.mAlCompanyFeed.get(Enterprise.company.getPositionOfCompanyFeedItem()).getmAlShotOfCompanyFeedItem().size()));
        mTvUserNameInPreviewCompanyFeed.setText("By " + CompanyFeedAsync.mAlCompanyFeed.get(Enterprise.company.getPositionOfCompanyFeedItem()).getUserName());
    }

    private void hideTopAndBottomLlInPreviewCompanyFeed () {
        mLlBottomInPreviewCompanyFeed.setVisibility(View.GONE);
        mLlTopInPreviewCompanyFeed.setVisibility(View.GONE);
    }

    private void showTopAndBottomLlInPreviewCompanyFeed () {
        mLlBottomInPreviewCompanyFeed.setVisibility(View.VISIBLE);
        mLlTopInPreviewCompanyFeed.setVisibility(View.VISIBLE);
    }

    // play video
    private void playVideoInPreviewCompanyFeed(String uri) {
        mVvVideoInPreviewCompanyFeed.setBackgroundColor(Color.TRANSPARENT);
        mVvVideoInPreviewCompanyFeed.setVideoURI(Uri.parse(uri));

        // Begin play video
        mVvVideoInPreviewCompanyFeed.start();
        mVvVideoInPreviewCompanyFeed.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                // show iv preview video
                mIvPreviewVideoInPreviewCompanyFeed.setVisibility(View.VISIBLE);
                // show ibtn play video
                mIbtnPlayVideoInPreviewCompanyFeed.setVisibility(View.VISIBLE);
                // hide video view
                mVvVideoInPreviewCompanyFeed.setVisibility(View.GONE);
            }
        });
    }
}
